﻿using System;
using System.Data;

namespace wuliuback
{
    public partial class ModifyUser : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            string user_id = Request["user_id"];
            string name = Request["name"];
            string mobile = Request["mobile"];
            string identityCard = Request["identity_card"];
            string license_plate = Request["license_plate"];
            string driving_license = Request["driving_license"];
            string vehicle_license = Request["vehicle_license"];
            string email = Request["email"];
            string address = Request["address"];
            string zip = Request["zip"];
            string company_name = Request["company_name"];
            string user_type = Request["user_type"];

            

            string code = "";
            string msg = "";
            string user = "";

            String sql = "";
            DbHelper dbHelper = new DbHelper();
            if (mobile == null || mobile.Length <= 0 || user_id==null || user_id.Length<=0)
            {
                code = "-1";
                msg = "电话或用户名为空";
                user = "\"\"";
                Response.Output.WriteLine("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"user\":" + user + "}");
            }

            sql = "select * from t_user where user_id = '" + user_id + "'";
            DataSet dataSet = dbHelper.Select(sql);
            DataTable table = dataSet.Tables[0];
            if (table.Rows.Count <= 0)
            {
                code = "-1";
                msg = "没有该用户名";
                user = "\"\"";
                Response.Output.WriteLine("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"user\":" + user + "}");
                return;
            }

            sql = "select * from t_user where mobile='" + mobile + "' and user_id <> '" + user_id + "'";
            dataSet = dbHelper.Select(sql);
            table = dataSet.Tables[0];
            if (table.Rows.Count <= 0)
            {
                sql = "update t_user set name='" + name  + "',mobile='" + mobile
                    + "',identity_card='" + identityCard 
                    + "',license_plate='" + license_plate + "',driving_license='" + driving_license + "',vehicle_license='" + vehicle_license
                    + "',email='" + email + "',address='" + address + "',zip='" + zip + "',company_name='" + company_name + "' "
                    + " where user_id='" + user_id+"'";
                if (dbHelper.Update(sql))
                {
                    sql = "select * from t_user where (mobile='" + mobile + "' or user_id='" + user_id + "')";
                    DataSet dataSet1 = dbHelper.Select(sql);
                    DataTable table1 = dataSet1.Tables[0];
                    if (table1.Rows.Count > 0)
                    {
                        code = "0";
                        user = JsonUtil.DataTableToJson(table1);
                    }
                    else
                    {
                        code = "-1";
                        user = "\"\"";
                    }
                }
                else
                {
                    code = "-1";
                    msg = "更新出错";
                    user = "\"\"";
                }
            }
            else
            {
                code = "-1";
                user = "\"\"";
                msg = "已存在改电话号码";
            }

            Response.Output.WriteLine("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"user\":" + user + "}");
        }

    }
}
